package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult getDish(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页查询,第一个是页码，第二个是每页显示记录数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //调用mapper方法，必须使用Page类来接收返回值
        Page<DishVO> page = dishMapper.select(dishPageQueryDTO);
        //通过page获取
        long total = page.getTotal();
        List<DishVO> list = page.getResult();
        return new PageResult(total,list);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getDishByDishId(Long id) {
        DishVO dishVO = dishMapper.selectDishById(id) ;
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectDishFlavorById(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    /**
     * 添加新菜品
     * @param dishDTO
     */
    @Override
    public void addDish(DishDTO dishDTO) {

        //添加一个新菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insertDish(dish);
    //获取insert后的主键值
        Long dishId = dish.getId();
        //添加多个口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor ->
                dishFlavor.setDishId(dishId));
        dishFlavorMapper.insertDishFlavor(flavors);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        //修改菜品
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        //修改菜品口味
        //删除该菜品下的所有口味
        Long dishId = dish.getId();
        dishFlavorMapper.deleteDishFlavor(dishId);
        //新增菜品口味
        if(CollectionUtils.isNotEmpty(dishDTO.getFlavors())){//对口味进行判空
            List<DishFlavor> flavors = dishDTO.getFlavors();
            log.info("口味为：{}", Arrays.toString(flavors.toArray()));
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dishId));
            dishFlavorMapper.insertDishFlavor(flavors);
        }

    }

    /**
     * 修改菜品的状态
     * @param status
     * @param id
     */
    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDish(dish);
    }

    /**
     * 批量删除菜品数据
     * @param ids
     */
    @Override
    public void deleteDish(String[] ids) {
        for(String id : ids){
            dishMapper.delete(id);
            dishFlavorMapper.deleteDishFlavor(Long.valueOf(id));
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> getDishByCategoryId(String categoryId) {
        List<DishVO> list = dishMapper.selectDishByCategoryId(categoryId);
        return list;
    }

}
